/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package z.h.w.jar.kit.code.pic;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import z.h.w.jar.kit.method.Foto;
import z.h.w.jar.kit.method.IO;
import z.h.w.jar.kit.method.Num;
import z.h.w.jar.kit.method.Roll;
import z.h.w.jar.kit.method.Self;
import z.h.w.jar.kit.method.To;

/**
 *
 * @author EnzoZhong
 */
public class String2Code implements Code {

      //操作对象
      private BufferedImage image;
      private Graphics2D g;
      //计算放到哪一格了
      private java.util.List<BufferedImage> imageList;
      //设置对象
      private String string;

      /**
       *
       * @param color
       * @param rangRoutate
       * @param rollHeight
       * @param fontSize
       * @param font
       * @param string
       */
      public String2Code ( Color color , Integer rangRoutate , Boolean rollHeight , Integer fontSize , Font font , String string ) {
            this ( To.objs2Array ( color ) , rangRoutate , rollHeight , To.objs2Array ( fontSize ) , To.objs2Array ( font ) , string );
      }

      /**
       *
       * @param colors
       * @param rangRoutate
       * @param rollHeight
       * @param fontSize
       * @param fonts
       * @param string
       */
      public String2Code ( Color[] colors , Integer rangRoutate , Boolean rollHeight , Integer[] fontSize , Font[] fonts , String string ) {
            this.string = string;
            Integer index = 0;
            imageList = new ArrayList<> ( 20 );
            Integer margin = Num.average ( fontSize ) / 10;
            /*
             * Character cha, Color color, Font font, Integer fontSize
             * 生成字符对应的图像映射
             */
            Character[] list = To.string2CharacterArray ( string );
            for ( Character chara : list ) {
                  Color color = Roll.roll ( colors );
                  Font font = Roll.roll ( fonts );
                  Integer size = Roll.roll ( fontSize );
                  Charaecter2Image pic = new Charaecter2Image ( chara , color , font , size );
                  pic.rotate ( Roll.range (  - rangRoutate , rangRoutate ) );
                  imageList.add ( pic.getImage () );
            }
            /*
             * 求出图片宽度和高度,并初始化图片,图片为PNG半透明的
             */
            Iterator<BufferedImage> iterator = imageList.iterator ();
            Double picWidth = 0.0;
            Integer picHeight = 0;
            while ( iterator.hasNext () ) {
                  BufferedImage bi = iterator.next ();
                  picWidth = picWidth + bi.getWidth () + margin;
                  picHeight = Num.max ( picHeight , bi.getHeight () );
            }
            Integer width = picWidth.intValue ();
            Integer height = picHeight.intValue ();
            //创建透明背景
            image = new BufferedImage ( width , height , BufferedImage.TYPE_4BYTE_ABGR );
            g = image.createGraphics ();

            Iterator<BufferedImage> itr = imageList.iterator ();
            while ( itr.hasNext () ) {
                  BufferedImage bi = itr.next ();
                  if ( rollHeight ) {
                        g.drawImage ( bi , null , index , Roll.range ( 0 , height - bi.getHeight () ) );
                  } else {
                        g.drawImage ( bi , null , index , height - bi.getHeight () );
                  }
                  index = index + bi.getWidth () + margin;
            }
      }

      /**
       *
       * @param times
       */
      public void drawNoise ( Integer times ) {
            for ( int x = 0 ; x < times ; x ++ ) {
                  if ( x % 2 == 0 ) {
                        g.drawArc ( Roll.range ( 0 , image.getWidth () ) , Roll.range ( 0 , image.getHeight () ) , x , x , x , x );
                  } else {
                  }
            }
      }

      /**
       *
       */
      public void drawNoise () {
      }

      /**
       *
       * @return
       */
      @Override
      public BufferedImage getImage () {
            g.dispose ();
            Foto.Border minMax = Foto.findBorder ( image );
            Point min = minMax.min;
            Point max = minMax.max;
            return image.getSubimage ( min.x , min.y , max.x - min.x + 1 , max.y - min.y + 1 );
      }

      /**
       *
       * @param args
       * @throws MalformedURLException
       * @throws NoSuchMethodException
       * @throws IllegalAccessException
       * @throws IllegalArgumentException
       * @throws InvocationTargetException
       * @throws FontFormatException
       * @throws IOException
       */
      public static void main ( String[] args ) throws MalformedURLException , NoSuchMethodException , IllegalAccessException , IllegalArgumentException , InvocationTargetException , FontFormatException , IOException {

            File[] files = new File ( "c:\\" ).listFiles ();
            for ( File file : files ) {

                  if ( file.toString ().contains ( ".png" ) ) {
                        file.delete ();
                  }
            }
            List<String> list = new ArrayList<> ( 20 );

            list.add ( "COME TI CHIAMI??" );
            list.add ( "WHAT IS YOUR NAME??" );
            list.add ( "QUANTI ANNI HAI??" );
            list.add ( "Earnestgroup欢迎你！" );

            for ( String str : list ) {
                  String2Code nc = new String2Code ( Roll.rollColor ( 100 ) , 0 , true , To.objs2Array ( 88 , 99 ) , Self.getChineseFonts () , str );
                  IO.bufferedImage2Png ( nc.getImage () , "c:\\" + System.currentTimeMillis () + ".png" );
            }
      }

      /**
       *
       * @return
       */
      @Override
      public String getString () {
            return string;
      }

}
